import redis

# 创建 Redis 连接对象
r = redis.Redis(host='localhost', port=6379, db=0)

# Redis 支持发布与订阅模式，允许客户端之间进行消息传递：

# 发布消息
r.publish('channel', 'Hello, Redis!')

# 创建订阅对象
pubsub = r.pubsub()

# 订阅频道
pubsub.subscribe('channel')

# 接收消息
for message in pubsub.listen():
    if message['type'] == 'message':
        print(f"Received message: {message['data'].decode('utf-8')}")